package com.glela.platform.service.impl;

import com.glela.platform.mapper.GivePromotionHistoryMapper;
import com.glela.platform.mapper.PromotionMapper;
import com.glela.platform.mapper.UserMapper;
import com.glela.platform.model.GivePromotionVo;
import com.glela.platform.model.MyattentionTalent;
import com.glela.platform.model.PromotionDetail;
import com.glela.platform.model.User;
import com.glela.platform.model.response.PromotionDetailVo;
import com.glela.platform.model.response.ResponsePromotionVo;
import com.glela.platform.service.UserPromotionGiftService;
import com.glela.platform.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("UserPromotionGiftService")
public class UserPromotionGiftServiceImpl implements UserPromotionGiftService {
    private static Logger logger = LoggerFactory
            .getLogger(UserPromotionGiftServiceImpl.class);

    @Autowired
    private GivePromotionHistoryMapper givePromotionHistoryMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PromotionMapper promotionMapper;

    @Override

    public MyattentionTalent selectGetPromotionRecordPage(String loginUserId, String inviterUserId, int startIndex, Integer pageSize) {
        List<Map<String, Object>> promotionMap = givePromotionHistoryMapper.selectTecherGivePromotionRecordPage(loginUserId, inviterUserId, startIndex, pageSize);
        //开始封装礼包对象
        MyattentionTalent myattentionTalent = new MyattentionTalent();
        User user1 = userMapper.selectUserEntityById(Long.parseLong(inviterUserId));
        myattentionTalent.setUserId(Long.parseLong(inviterUserId));
        myattentionTalent.setNickName(user1.getNickName());
        myattentionTalent.setUserName(user1.getUserName());
        myattentionTalent.setPicture(user1.getPicture());
        myattentionTalent.setCreateTime(null == user1.getLastBindTime()?"":user1.getLastBindTime().substring(0,19));
        ArrayList<GivePromotionVo> givePromotionVoList = new ArrayList<GivePromotionVo>();
        if (null != promotionMap && !promotionMap.isEmpty()) {
            for (Map<String, Object> map : promotionMap) {
                GivePromotionVo vo = new GivePromotionVo();
                vo.setId(Long.parseLong(map.get("id").toString()));
                vo.setUserId(Long.parseLong(map.get("user_id").toString()));
                vo.setTearchUserId(Long.parseLong(map.get("tearch_user_id").toString()));
                vo.setPromotionId(Long.parseLong(map.get("promotion_id").toString()));
                vo.setName(map.get("promotionName").toString());
                vo.setGiveTime(DateUtils.DateToString((Date) map.get("give_time"), DateUtils.DATE_TIME_FORMAT));
                //判断礼包的状态（未领取，已领取，已过期）
                long nowTime = new Date().getTime();
                Date date = (Date) map.get("expiration_time");
                if (null != map.get("get_time")) {
                    vo.setStatus("1");//已领取
                } else if (null == map.get("get_time") && date.getTime() - nowTime > 0) {//没有领取并且未过期
                    vo.setStatus("2");//未领取
                } else if (date.getTime() - nowTime < 0) {
                    vo.setStatus("3");//已过期
                }
                givePromotionVoList.add(vo);
            }
        }
        myattentionTalent.setGivePromotionVoList(givePromotionVoList);
        return myattentionTalent;
    }

    @Override
    public List<ResponsePromotionVo> selectGiftPromotionListByteacherId(String loginUserId) {
        List<ResponsePromotionVo>  resultList =  promotionMapper.selectGiftPromotionList();
        for (ResponsePromotionVo responsePromotionVo : resultList) {
            responsePromotionVo.setTotalNum(responsePromotionVo.getPromotionDetails().size());
            //计算优惠券总面值
            int totalAmount = 0;
            for (PromotionDetailVo promotionDetail : responsePromotionVo.getPromotionDetails()) {
                totalAmount = totalAmount + promotionDetail.getAmount();
            }
            responsePromotionVo.setTotalAmount(String.valueOf(totalAmount/100));
        }
        return resultList;
    }

    @Override
    public List<ResponsePromotionVo> selectGiftPromotionListByteacherId(String loginUserId, String inviterUserId) {
        return  promotionMapper.selectGiftPromotionList1(loginUserId,inviterUserId);
    }
}
